package cn.com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @PackageName: cn.com.zpark.servlet_context
 * @ClassName: ServletContextDemo02
 * @Description:
 * @author: RZS
 * @date: 2021/11/10  17:59
 */
@WebServlet("/scd2.html")
public class ServletContextDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取context对象
        ServletContext context = getServletContext();
        // 根据参数名字获取参数值
        String name = context.getInitParameter("name");
        String age = context.getInitParameter("age");
        System.out.println(name+": "+age);

        System.out.println("*****************************");

        // 通过获取参数名字枚举集合获取参数值
        Enumeration<String> names = context.getInitParameterNames();
        while (names.hasMoreElements()) {
            // 获取参数名字
            String key = names.nextElement();
            String val = context.getInitParameter(key);
            System.out.println(key+": "+val);
        }

    }
}
